{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# List Comprehensions [demonstration]\n",
    "One of Python's most \"Pythonic\" features is the **list comprehension**. It's a way of creating a list in a compact and, once you get used to them, readable way. See the demonstrations below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numbers 0 to 9 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "numbers_0_to_9 = [x for x in range(10)]\n",
    "print(\"Numbers 0 to 9\", numbers_0_to_9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Squares        [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n"
     ]
    }
   ],
   "source": [
    "squares = [x * x for x in range(10)]\n",
    "print(\"Squares       \", squares)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Odds           [1, 3, 5, 7, 9]\n"
     ]
    }
   ],
   "source": [
    "# note - this example uses the \"modulo\" operator\n",
    "\n",
    "odds = [x for x in range(10) if x % 2 == 1]\n",
    "print(\"Odds          \", odds)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Advanced List Comprehensions [optional]\n",
    "If you're already an advanced programmer, you may be interested in some of these more advanced list comprehensions. They're one of the things that experienced Python users learn to love about Python.\n",
    "\n",
    "This example also uses a data type called a namedtuple which is similar to a `struct` data type in other languages."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:   Andy\n",
      "age:    30\n",
      "gender: m\n"
     ]
    }
   ],
   "source": [
    "from collections import namedtuple\n",
    "Person = namedtuple(\"Person\", [\"name\", \"age\", \"gender\"])\n",
    "\n",
    "people = [\n",
    "    Person(\"Andy\", 30, \"m\"),\n",
    "    Person(\"Ping\", 1, \"m\"), \n",
    "    Person(\"Tina\", 32, \"f\"),\n",
    "    Person(\"Abby\", 14, \"f\"),\n",
    "    Person(\"Adah\", 13, \"f\"),\n",
    "    Person(\"Sebastian\", 42, \"m\"),\n",
    "    Person(\"Carol\" , 68, \"f\"),\n",
    "]\n",
    "\n",
    "# first, let's show how this namedtuple works.\n",
    "\n",
    "andy = people[0]\n",
    "\n",
    "print(\"name:  \", andy.name)\n",
    "print(\"age:   \", andy.age)\n",
    "print(\"gender:\", andy.gender)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Male names: ['Andy', 'Ping', 'Sebastian']\n"
     ]
    }
   ],
   "source": [
    "# now let's show what we can do with a list comprehension\n",
    "\n",
    "male_names = [person.name for person in people if person.gender==\"m\"]\n",
    "print(\"Male names:\", male_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "teen_names = [p.name for p in people if 13 <= p.age <= 18 ]\n",
    "print(\"Teen names:\", teen_names)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
